<?php

class StorageManagerController extends Controller {

	protected $db;

	function __construct($db) {
	
		$this->db = $db;
		
		if (isset($_GET['view'])) {
		
			$page = 1;
			
			if (isset($_GET['pageNum'])) {
				$page = $_GET['pageNum'];
			}
			
			$viewItem = $_GET['view'];
			if ($viewItem == 'movieList') {
				$movies = $this->fetchMovies($page, 10);
				$this->view = new MovieListView();
				$this->view->setMovieList($movies);
				$this->view->setTitle('Movies');
			}
			else if ($viewItem == 'insertMovie') {
				if (isset($_POST['action'])) {
					if ($_POST['action'] == "save") {
						$movie = new Movie(-1, $_POST['title'], $_POST['year'], $_POST['directorID'], '', '', $_POST['rating'], $_POST['noCopies'], $_POST['price']);
						if (!$this->insertMovie($movie, $_POST['catId'], $_SESSION['actorLines'])) {
							$directorList = $this->fetchDirectors();
							$actorList = $this->fetchActors();
							$catList = $this->getCategories();
							$this->view = new MovieInsertView();
							$this->view->setTitle('Insert New Movie');
							$this->view->setDirectorsList($directorList);
							$this->view->setActorsList($actorList);
							$this->view->setCategoryList($catList);
							$this->view->setError('<p style="color:red">An error occured. Could not store movie</p>');
							return;
						}
						$this->view = new ConfirmationMessageView("<p>Movie has been stored successfully.</p>");
						$this->view->setTitle("New movie stored");
						return;
					}
					else if ($_POST['action'] == "actor_save") {
						if (isset($_SESSION['actorLines'])) {
							$temp = array();
							$temp[0] = $_POST['actorID'];
							$temp[1] = $_POST['isMajor'];
							$actorLine = implode('_', $temp);
							$_SESSION['actorLines'] = $_SESSION['actorLines'].'[]'.$actorLine;
						}
						else {
							$i = 0;
							$temp = array();
							$temp[0] = $_POST['actorID'];
							$temp[1] = $_POST['isMajor'];
							$actorLine = implode('_', $temp);
							$_SESSION['actorLines'] = $actorLine;
						}
					}
				}
				$directorList = $this->fetchDirectors();
				$actorList = $this->fetchActors();
				$catList = $this->getCategories();
				$this->view = new MovieInsertView();
				$this->view->setTitle('Insert New Movie');
				$this->view->setDirectorsList($directorList);
				$this->view->setActorsList($actorList);
				$this->view->setCategoryList($catList);
				
			}
			else if ($viewItem == 'awards') {
				$m = 10;
				if (isset($_GET['m'])) {
					$m = $_GET['m'];
				}
				
				$bestClients = $this->getBestClients($m);
				$this->view = new AwardsView();
				$this->view->setBestClients($bestClients);
				$this->view->setM($m);
				$this->view->setTitle('Top '.$m.' clients');
			}
			else if ($viewItem == 'statistics') {
				$m = 10;
				if (isset($_GET['m'])) {
					$m = $_GET['m'];
				}
				$mostPopularMovies = $this->fetchMostPopularMovies($m);
				$mostPopularDirectors = $this->fetchMostPopularDirectors($m);
				$mostPopularActors = $this->fetchMostPopularActors($m);
				$this->view = new StatisticsView();
				$this->view->setMostPopularMovies($mostPopularMovies);
				$this->view->setMostPopularDirectors($mostPopularDirectors);
				$this->view->setMostPopularActors($mostPopularActors);
				$this->view->setTitle('Statistics');
			}
			else if ($viewItem == 'editMovie') {
				if (isset($_GET['movieId'])) {
					$movieId = $_GET['movieId'];
				}
				if (isset($_GET['movieName'])) {
					$movieName = $_GET['movieName'];
				}
				if (isset($_GET['noCopies'])) {
					$noCopies = $_GET['noCopies'];
				}
				if (isset($_POST['action'])) {
					if ($_POST['action'] == "save") {
						if (isset($_POST['noCopies'])) {
							if ($this->updateCopies($_POST['movieId'], $_POST['noCopies'])) {
							
								$this->view = new ConfirmationMessageView("<p>Movie copies have been updated successfully.</p>");
								$this->view->setTitle("Updated movie copies");
							}
							else {
								$this->view = new EditMovieView();
								$this->view->setMovieId($movieId);
								$this->view->setMovieName($movieName);
								$this->view->setNoCopies($noCopies);
								$this->view->setActorsPlayed($this->getMovieActors($movieId));
								$this->view->setError("<p style='color:red;'>An error occured while trying to update copies</p>");
							}
							return;
						}
					}	
				}
				$this->view = new EditMovieView();
				$this->view->setMovieId($movieId);
				$this->view->setMovieName($movieName);
				$this->view->setNoCopies($noCopies);
				$this->view->setActorsPlayed($this->getMovieActors($movieId));
				
			}
			else {
				//error message or 404 page
			}
		}
		else {
			//error message or 404 page
		}
	}
	
	function fetchMovies($page, $limit) {
		$results = $this->db->getMovies($page, $limit);
		$movieList = array();
		$i=0;
		while ($row = $results->fetch_row()) {
			$movie = new Movie($row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6], $row[7], $row[8]);
			$movieList[$i] = $movie;
			$i++;
		}
		return $movieList;
	}
	
	function insertMovie($movie, $catId, $actors) {
		return $this->db->addMovie($movie->getTitle(), $movie->getYear(), $movie->getDirectorID(), $movie->getRating(), $movie->getNoCopies(), $movie->getPrice(), $catId, $actors);
	}
	
	
	
	function fetchDirectors() {
		$results = $this->db->getDirectors();
		$dirList = array();
		$i=0;
		while ($row = $results->fetch_row()) {
			$dir = new Director($row[0], $row[1], $row[2]);
			$dirList[$i] = $dir;
			$i++;
		}
		return $dirList;
		
	}
	
	function fetchActors() {
		$results = $this->db->getActors();
		$dirList = array();
		$i=0;
		while ($row = $results->fetch_row()) {
			$dir = new Actor($row[0], $row[1], $row[2], null);
			$dirList[$i] = $dir;
			$i++;
		}
		return $dirList;
		
	}
	
	function updateCopies($m, $c) {
		return $this->db->updateCopies($m, $c);
	}
	
	function fetchMostPopularMovies($m) {
		$results = $this->db->getMostPopularMovies($m);
		$mList = array();
		$i=0;
		while ($row = $results->fetch_row()) {
			$movie = new Movie($row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6], $row[7], $row[8]);
			$mList[$i] = $movie;
			$i++;
		}
		return $mList;
	}
	
	function fetchMostPopularActors($m) {
		$results = $this->db->getMostPopularActors($m);
		$aList = array();
		$i=0;
		while ($row = $results->fetch_row()) {
			$a = new Actor($row[0], $row[1], $row[2], $row[3]);
			$aList[$i] = $a;
			$i++;
		}
		return $aList;
	}
	
	function fetchMostPopularDirectors($m) {
		$results = $this->db->getMostPopularDirectors($m);
		$aList = array();
		$i=0;
		while ($row = $results->fetch_row()) {
			$a = new Director($row[0], $row[1], $row[2]);
			$aList[$i] = $a;
			$i++;
		}
		return $aList;
	}
	
	function getBestClients($m) {
		$results = $this->db->getBestClients($m);
		if (!$results) {
			return false;
		}
		$mList = array();
		$i=0;
		while ($row = $results->fetch_row()) {
			$a = new Member($row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6], $row[7]);
			$mList[$i] = $a;
			$i++;
		}
		return $mList;
	}
	
	function getCategories() {
		$results = $this->db->getCategories();
		$mList = array();
		$i=0;
		while ($row = $results->fetch_row()) {
			$cat = new Category($row[0], $row[1]);
			$mList[$i] = $cat;
			$i++;
		}
		return $mList;
	}
	
	function getMovieActors($mId) {
		$results = $this->db->getMovieActors($mId);
		$aList = array();
		$i=0;
		while ($row = $results->fetch_row()) {
			$actor = new Actor($row[0], $row[1], $row[2], $row[3]);
			$aList[$i] = $actor;
			$i++;
		}
		return $aList;
	}

}

?>